import request from '@/utils/request'
import * as URL from '@/constants/api'
import dayjs from 'dayjs';

function createArrItem (d, idx) {
  const now = Date.now();
  return new Array(100).fill(0).map((d, i) => {
    return {
      _id: now - 60000 * (99 - i) - 6000000 * (999 - idx),
      api_count: Math.random() * 10,
    };
  })
}

// 首页折线图
export function getLineData () {
  return request({
    url: URL.API_COUNT
  })
    // .then(res => res.data.data.result)
    .then(() => new Array(1000).fill(0).map(createArrItem))
    .then(res => res.map(d => ({
      time: dayjs(d.slice(-1)[0]._id).format('HH:mm'),
      count: d.reduce((p, c) => p + c.api_count, 0),
    })))
    // 1000 >>> 10
    .then(res => {
      // i: 0 >>> { time: , count: 500 }
      // 0 ~ 99
      // i: 1
      // 100 ~ 199
      return new Array(10).fill(0).map((d, i) => {
        return {
          time: res[100 * i].time,
          count: res.slice(i * 100, i * 100 + 100).reduce((p, c) => p + c.count, 0),
        };
      });
    });
}
